﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Benefit.Models.Sys;
using System.Data.Entity;
namespace Benefit.Service.Day
{
    public class ServiceCompanyDayChange : Benefit.Interface.Day.ICompanyDayChange
    {
        Benefit.DB.DBManager db = null;
        public ServiceCompanyDayChange(Benefit.DB.DBManager db)
        {
            this.db = db;
        }


        /// <summary>
        /// 删除当日所有记录
        /// </summary>
        /// <param name="weekId"></param>
        public void Delete(int tradeHistoryId)
        {
            var query = from t in db.CompanyDayChange where t.TradeHistoryId == tradeHistoryId select t;
            foreach (Models.Day.CompanyDayChange c in query)
            {
                db.CompanyDayChange.Remove(c);
            }
            db.SaveChanges();
        }

        /// <summary>
        /// 保存日均值
        /// </summary>
        /// <param name="tradeHistoryId">插入时间编号</param>
        /// <param name="pdate">插入时间</param>
        public void InitCompanyDayChange(int tradingHistoryId)
        {
            Interface.ServerData.IT_Account ita = new ServerData.ServiceT_Account(db);
            List<Models.ServerData.T_Account> serverAccounts = ita.GetList();
            Models.Day.CompanyDayChange change = new Models.Day.CompanyDayChange();
            var query = (from t in db.CompanyDayChange where t.TradeHistoryId == tradingHistoryId select t).ToList();
            var query2 = (from t in db.AccountDayChange where t.TradeHistoryId == tradingHistoryId select t).ToList();
            var query3 = (from t in db.AccountDayAnalysis where t.TradeHistoryId == tradingHistoryId select t).ToList();
            Interface.Sys.IOperator iop = new Sys.ServiceOperator(db);
            var operators = iop.GetUnDeletedOperators();

            if (query.Count() > 0)//有本日数据
            {
                change = query.First();
            }
            if (query2.Count() > 0)
            {
                change.BillCount = query2.Sum(a => a.BillCount);
                change.DayCount = query2.Sum(a => a.DayCount);
                change.Free = query2.Sum(a => a.Free);
                change.LoseCount = query2.Sum(a => a.LoseCount);
                change.Profit = query2.Sum(a => a.Profit);
                change.SumLost = query2.Sum(a => a.SumLost);
                change.SumWin = query2.Sum(a => a.SumWin);
                change.UsedMargin = query2.Sum(a => a.UsedMargin);
                change.WinCount = query2.Sum(a => a.WinCount);
            }
            if (query3.Count() > 0)
            {
                change.AvgBillBenefit = query3.Sum(a => a.AvgBillBenefit);
                change.AvgPostionTime = query3.Sum(a => a.AvgPostionTime);
                change.MaxReturn = query3.Sum(a => a.MaxReturn);
                change.OverStopLoss = query3.Sum(a => a.OverStopLoss);
                change.PingJinPing = query3.Sum(a => a.PingJinPing);
                change.RaiseRate = query3.Sum(a => a.RaiseRate);
                change.RiskCount = query3.Sum(a => a.RiskCount);
                change.Utilization = query3.Sum(a => a.Utilization);
                change.WinRate = query3.Sum(a => a.WinRate);
                change.Yield = query3.Sum(a => a.Yield);
            }
            change.TradeHistoryId = tradingHistoryId;
            if (query.Count() == 0)//没有本月数据
            {
                db.CompanyDayChange.Add(change);
            }
            db.SaveChanges();
        }



        /// <summary>
        /// 获取公司日总值
        /// </summary>
        /// <param name="accountid"></param>
        /// <param name="tradeHistoryId"></param>
        /// <returns></returns>
        public Models.Day.CompanyDayChange GetCompanyDayChange(int tradeHistoryId)
        {
            Models.Day.CompanyDayChange count = new Models.Day.CompanyDayChange();
            var query = db.CompanyDayChange.Where(a => a.TradeHistoryId == tradeHistoryId);
            if (query.Count() > 0)
            {
                count = query.First();
            }
            return count;
        }

        /// <summary>
        /// 获取公司日总值
        /// </summary>
        /// <param name="accountid"></param>
        /// <param name="tradeHistoryId"></param>
        /// <returns></returns>
        public List<Models.Day.CompanyDayChange> GetCompanyDayChange(int month, int year)
        {
            var query = db.CompanyDayChange.Where(a => a.TradeHistory.MPdate == month).Where(a => a.TradeHistory.YPdate == year).Include(a => a.TradeHistory).OrderBy(a => a.TradeHistory.DPdate);
            return query.ToList();
        }
    }
}
